<?php

namespace app\user\controller;

use think\Controller;

use think\Db;
use think\Request;
use think\Session;


class UserCenter extends Mycontroller
{
    //
    public function index()
    {
        $uid=Session::get('user.id');
        $res=Db::name('user_data')->where('uid',$uid)->find();
        //存入redis 下回从redis获取
//        var_dump($res);

        //redis获取原信息
        $data=Session::get('user');
//        var_dump($data);
        return view('user/userCenter',['res'=>$res,'data'=>$data]);

    }

    public function edittel()
    {
        $data=Session::get('user');
        return view('user/edittel',['data'=>$data]);
    }

    public function doedittel(Request $request)
    {
        $uid=Session::get('user.id');
        $post=$request->post();
        $telcode=Session::get('temp.telcode');
//        var_dump($telcode);
//        var_dump($post);
        if($telcode!=$post['code']){
            return "<script>alert('验证码错误');history.go(-1);</script>";
        }
        $res=Db::name('user')->where('id',$uid)->update(['tel'=>$post['newmobile']]);
//        var_dump($res);
        if($res){
            Session::clear();
            return "<script>alert('修改成功,请重新登入');window.location.href='".url('user/Login/index')."'</script>";
//            $this->redirect('user/Login/index');
//            $this->success('修改成功,请重新登入',url('user/Login/index'));
        }else{
            return "<script>alert('修改失败,请稍后重试');history.go(-1);</script>";
        }

    }

    public function shiming()
    {
        return view('user/shiming');

    }

    public function doshiming(Request $request)
    {
        $uid=Session::get('user.id');
        $post=$request->post();
        $post['smstatus']=2;
//        var_dump($post);
        $res=Db::name('user_data')->where('uid',$uid)->update($post);
        if($res){
            return "<script>alert('正在提交审核');window.location.href='".url('user/UserCenter/index')."'</script>";
        }else{
            return "<script>alert('提交失败,请稍后重试');history.go(-1);</script>";
        }
    }

    public function editloginpwd()
    {
        return view('user/editloginpwd');
    }

    public function doeditloginpwd(Request $request)
    {
        $uid=Session::get('user.id');
        $post=$request->post();
//        var_dump($post);
        $oldpwd=Db::name('user')->field('pwd')->where('id',$uid)->find()['pwd'];
//        var_dump($oldpwd);
        if($oldpwd!=md5($post['oldpwd'])){
            return "<script>alert('原密码错误');history.go(-1);</script>";
        }
        $res=Db::name('user')->where('id',$uid)->update(['pwd'=>md5($post['newpwd'])]);
        if($res){
            Session::clear();
            return "<script>alert('修改成功,请重新登入');window.location.href='".url('user/Login/index')."'</script>";
        }else{
            return "<script>alert('修改失败,请稍后重试');history.go(-1);</script>";
        }
    }

    public function editzfpwd()
    {
        return view('user/editzfpwd');
    }

    public function doeditzfpwd(Request $request)
    {
        $uid=Session::get('user.id');
        $post=$request->post();
//        var_dump($post);die;
        $oldpwd=Db::name('user_data')->field('zf_password')->where('uid',$uid)->find()['zf_password'];
        if($oldpwd!=''){
            if($oldpwd!=md5($post['oldpwd'])){
                return "<script>alert('原密码错误');history.go(-1);</script>";
            }
        }
        $res=Db::name('user_data')->where('uid',$uid)->update(['zf_password'=>md5($post['newpwd'])]);
        if($res){
            return "<script>alert('修改成功');window.location.href='".url('user/UserCenter/index')."'</script>";
        }else{
            return "<script>alert('修改失败,请稍后重试');history.go(-1);</script>";
        }
    }

    public function mymsg()
    {
        $uid=Session::get('user.id');
        $data=Db::table(['__USER_MSG__'=>'a','__MSG__'=>'b'])->where('a.uid',$uid)->where('a.mid=b.id')->order('time asc')->select();
//        var_dump($data);
        Db::name('user_msg')->where('uid',$uid)->update(['status'=>1]);


        return view('user/mymsg',['data'=>$data]);
    }

    public function rj()
    {
        return view('user/rj');
    }

    public function dorj(Request $request)
    {
        $post=$request->post();
        $uid=Session::get('user.id');
        // 原金额
        $yj=Db::name('user_data')->where('uid',$uid)->find()['money'];
        $xje=$post['rj']+$yj;
        $res=Db::name('user_data')->where('uid',$uid)->update(['money'=>$xje]);
        if($res){
            return "<script>alert('入金成功');window.location.href='".url('user/UserCenter/index')."'</script>";
        }else{
            return "<script>alert('入金失败,请稍后重试');history.go(-1);</script>";
        }

    }

    public function cj()
    {
        return view('user/cj');
    }

    public function docj(Request $request)
    {
        $post=$request->post();

        $uid=Session::get('user.id');
        $data=Db::name('user_data')->where('uid',$uid)->find();
        $yj=$data['money'];
        $zfpwd=$data['zf_password'];
        if(!$zfpwd){
            return "<script>alert('请先设置出金密码');window.location.href='".url('user/UserCenter/editzfpwd')."'</script>";
        }
        if(md5($post['cjpwd'])!=$zfpwd){
            return "<script>alert('密码错误,请重试');history.go(-1);</script>";
        }
        if($post['cj']>$yj){
            return "<script>alert('余额不足,请输入正确的金额');history.go(-1);</script>";
        }
        $xje=$yj-$post['cj'];
        $res=Db::name('user_data')->where('uid',$uid)->update(['money'=>$xje]);

        if($res){

            $mid=Db::name('msg')->insertGetId(['msg'=>'您有'.$post['cj'].'元的金额提现已到账','time'=>time()]);
            $res=Db::name('user_msg')->insert(['mid'=>$mid,'uid'=>$uid,'status'=>'2']);
        }else{
            return "<script>alert('出金失败,请稍后重试');history.go(-1);</script>";
        }
        


        if($res){
            return "<script>alert('出金成功');window.location.href='".url('user/UserCenter/index')."'</script>";
        }else{
            return "<script>alert('出金失败,请稍后重试');history.go(-1);</script>";
        }



    }

}
